# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: mux_test.py
  @time: 2022/7/23 14:21
  @desc:
"""
import pyvisa as visa
from ATS.application.LASER_App.N7744C_App import N7744C_App
from ATS.application.LASER_App.N7786C_App import N7786C_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
from ATS.proberdriver.cascade_prober import CascadeProber
from ATS.application.LCR_App.U2722A_App import U2722A_App
from matplotlib import pyplot as plt
import numpy as np
import time

vl1 = 0.7
vh1 = 1.325
vl2 = 0.65
vh2 = 1.225
vl3 = 0.85
vh3 = 1.35
cp = CascadeProber("192.168.1.100")
n7744c_app = N7744C_App("SS2")
# print(n7744c_app)
l8164b_app = L8164B_App("SS3")
n7786_app = N7786C_App("SS1")

heater1_smu_address = "USB0::0x0957::0x4118::MY60179001::0::INSTR"  # 2通道
heater3_smu_address = "USB0::0x0957::0x4118::MY61300001::0::INSTR"  # 3通道
heater2_smu_address = "USB0::0x0957::0x4118::MY60179001::0::INSTR"  # 1通道


def get_step_array(start, stop, step):
    array_list = np.arange(start, stop, step)
    array_list = [str(i) for i in array_list]
    array_list.append(str(stop))
    return array_list


def plot_curve(x_y_list):
    for x_y in x_y_list:
        plt.plot(x_y[0], x_y[1])
        plt.xlabel("WaveLength")
        plt.ylabel("Power")
        plt.grid(True)
    plt.show()


def oo_te_alignment(n7744c_app, l8164b_app, n7786c_app, max_min=1):
    try:
        # max_min: 0代表显示最小,1代表显示最大
        global polarization_data

        n7744c_app.set_logging_state("STOP")
        # l8164b_app.cls()
        l8164b_app.set_wave_length(1311)
        l8164b_app.set_power_unit()
        l8164b_app.set_power_value(10)
        time.sleep(4)
        l8164b_app.output_on()
        n7744c_app.reset()
        n7744c_app.set_power_range(10)
        # print("power_range_after")
        n7744c_app.set_auto_gain()
        # print("set_auto_gain")
        n7744c_app.trigger_input_mode()
        n7744c_app.set_power_wave_length(1311)
        n7744c_app.set_logging_points()
        n7786c_app.reset()
        n7786c_app.set_stabilization_mode(0)
        n7786c_app.set_random_points_stepwidth()
        n7786c_app.set_trigger_conf_mode(4)
        n7786c_app.set_sample_point_rate()
        n7786c_app.set_wave_length(1311)
        n7786c_app.start_polarimeter_logging()
        n7786c_app.start_polarization_sequence()
        n7744c_app.set_logging_state("START")
        n7786c_app.trigger()
        n7744c_app.tigger_status()
        while True:
            state = n7744c_app.get_func_state()
            if state == "LOGGING_STABILITY,COMPLETE":
                result_list = n7744c_app.get_func_res()
                # print("result", result_list)
                break
        max_index = result_list.index(max(result_list))
        min_index = result_list.index(min(result_list))
        while True:
            log_value = n7786c_app.get_logged_value()
            if log_value == "+0":
                polarization_data = n7786c_app.get_polarization_sweep_data()
                break
        n7786c_app.stop_logging_polcontroller()
        state = n7786c_app.get_opc_state()
        if state == "1":
            n7786c_app.set_external_trigger_delay()
            n7786c_app.set_trigger_conf_mode()
            n7786c_app.set_stabilization_mode(1)

        max_polar = [polarization_data[max_index * 3], polarization_data[max_index * 3 + 1],
                     polarization_data[max_index * 3 + 2]]
        min_polar = [polarization_data[min_index * 3], polarization_data[min_index * 3 + 1],
                     polarization_data[min_index * 3 + 2]]
        print(max_polar)
        if max_min:
            n7786c_app.set_polarization_value(*max_polar)
        else:
            n7786c_app.set_polarization_value(*min_polar)
    except Exception as e:
        print("oo_te_alignment" + str(e))


def spectral_scan():
    # cp.move_optical_subdie_pos("reference", "H1")
    # cp.move_optical_subdie_pos("reference", "H2")
    cp.area_scan()
    oo_te_alignment(n7744c_app, l8164b_app, n7786_app)
    # n7744c_app.set_logging_state("STOP")
    # x_data = get_step_array(1260, 1360, 1)
    # x_data = [float(i) for i in x_data]
    # points = len(x_data)
    # l8164b_app.set_power_unit()
    # l8164b_app.set_power_value(10)
    # time.sleep(3)
    # p_in = l8164b_app.fetch_input_power()
    # l8164b_app.set_sweep_mode()
    # l8164b_app.set_repeat_mode()
    # l8164b_app.set_wave_sweep_speed()
    # l8164b_app.set_start_stop_step_wave(1260, 1360, 1)
    # l8164b_app.trigger_config()
    # l8164b_app.trigger_output()
    # l8164b_app.trigger_input()
    # l8164b_app.output_on()
    # l8164b_app.set_wave_sweep_state()
    # # n7744c_app.reset()
    # n7744c_app.set_power_unit()
    # n7744c_app.set_power_range(10)
    # n7744c_app.set_auto_gain()
    # n7744c_app.set_power_atim()
    # n7744c_app.trigger_input_mode()
    # n7744c_app.set_power_wave_length()
    # n7744c_app.set_logging_points(points)
    # n7744c_app.set_logging_state()
    # n7744c_app.tigger_status()
    # while True:
    #     state = n7744c_app.get_func_state()
    #     if state == "LOGGING_STABILITY,COMPLETE":
    #         y_data = n7744c_app.get_func_res()
    #         break
    # y_data = [float(i) for i in y_data]
    # # print(y_data)
    # l8164b_app.stop_sweep()
    # return y_data


def p_λ():
    heater1 = U2722A_App("SMU2")
    heater3 = U2722A_App("SMU3")

    heater1.set_voltage(vl1, 2)
    heater3.set_voltage(vh3, 3)
    heater1.set_voltage_range(2)
    heater1.set_current_range_limit("120mA", 0.12, 2)
    heater3.set_voltage_range(3)
    heater3.set_current_range_limit("120mA", 0.12, 3)
    heater1.output_state(1, 2)
    heater3.output_state(1, 3)

    wave_list = get_step_array(1260, 1360, 1)
    n7744c_app.reset()
    n7744c_app.set_power_unit()
    n7744c_app.set_power_range_auto()
    n7744c_app.set_power_atim()
    power_list = []
    for wave in wave_list:
        l8164b_app.set_wave_length(wave)
        power_value = float(n7744c_app.get_power_value())
        power_list.append(power_value)
    heater1.output_state(0, 2)
    heater3.output_state(0, 3)
    wave_list = [float(i) for i in wave_list]
    return [wave_list, power_list]


x_y_list = []
for i in range(1):
    print("i", i)
    spectral_scan()
    # cp.move_optical_subdie_pos("M3", "H2")
    # cp.move_optical_subdie_pos("M3", "H1")
    # cp.area_scan()
    wave_list, power_list = p_λ()
    # power_list = [power_list[idx] - y_data[idx] for idx in range(len(power_list))]
    x_y_list.append([wave_list, power_list])
    # print(x_y_list)

plot_curve(x_y_list)
